from django.template import Library
import logging

register = Library()

@register.filter
def join_by_name(value):
    return ', '.join(map(lambda x: x['name'] if x.has_key('name') else '', value))

@register.filter
def millions(value):
    try:
        v = int(value)
    except:
        return '-'
    return int(round(v / 1000000.0, 0))

@register.filter
def join_indication_or_disease(value):
    a = []
    for d in value:
        logging.error('D: %s' % d)
        if d.has_key('disease') and isinstance(d['disease'], dict):
            if d['disease']['name']:
                a.append('%s for %s' % (d['phase_of_development'], d['disease']['name']))
    return '; '.join(a)
